package com.kim.net.netState;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetStateUtil {

	public static final int DISCONNECT = 0;// 未连接
	public static final int CONNECT = 1;// 连接

	public static final int WIFI = 1;// WIFI网络类型
	// public final int _2G = 2;
	// public final int _3G = 3;
	public static final int GPRS = 2;// 手机2G/3G网络类型

	public static final int UNKNOW = 0;// 未知网络类型

	private static int netState = 0;// 联网状态

	private static int netType = 0;// 网络类型

	private static void setNetState(Context context) {

		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connManager.getActiveNetworkInfo();

		if (info != null && info.isConnected()) {
			// 判断当前网络是否已经连接
			if (info.getState() == NetworkInfo.State.CONNECTED) {

				netState = CONNECT;
				if (ConnectivityManager.TYPE_MOBILE == info.getType()) {

					netType = GPRS;

				} else {

					netType = WIFI;
				}

			} else {

				netState = DISCONNECT;

				netType = UNKNOW;

			}

		} else {

			netState = DISCONNECT;

			netType = UNKNOW;

		}

	}

	public static void initNetState(Context context) {
		setNetState(context);

	}

	public static int getNetState() {

		return netState;
	}

	public static int getNetType() {

		return netType;

	}

}
